package com.movisol.messengercommunicator.asynctasks;

import com.movisol.messengercommunicator.controllers.CommunicatorController;
import com.movisol.messengercommunicator.helper.MessengerCommunicatorHelper;
import com.movisol.messengerprofileservice.interfaces.errorcodes.SaveUserProfileErrorCodes;
import com.movisol.tools.HelperUtils;

import android.os.AsyncTask;
import android.widget.Toast;

/**
 * Asynchronous loads the messages list of the selected conversation
 * @author Antonio Santiago
 *
 */
public class AsyncLoadMessagesList extends AsyncTask<Void, Void, Void> {
	
	private CommunicatorController cc = CommunicatorController.getInstance();

	@Override
	protected Void doInBackground(Void... params) {
		
		String result =  cc.getXMLfromUri(cc.getMessagesCache().getUri());
		cc.setMessagesList(MessengerCommunicatorHelper.parseXMLConversationMessagesListToObject(result));
//		for(Message message : messagesList.getMessages())
//			currentMessagesList.put(message.getMessagePublicId(), message);
		
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {

		super.onPostExecute(result);
		
		if(cc.getMessagesList() != null)
			cc.fireMessagesListLoaded(null);
		else
		{
//			HelperUtils.showToastMessage(cc.getContext(), SaveUserProfileErrorCodes.getErrorByCode(SaveUserProfileErrorCodes.INTERNAL_ERROR), Toast.LENGTH_LONG);
			HelperUtils.LogE(getClass(), "cc.getMessagesList() is NULL");
			cc.fireMessagesListLoaded(SaveUserProfileErrorCodes.getErrorByCode(SaveUserProfileErrorCodes.INTERNAL_ERROR));
		}
	}
	
	

}
